package br.ufal.tci.nexos.arcolive.service.broker.command;

import br.ufal.tci.nexos.arcolive.ArCoLIVEServiceManager;
import br.ufal.tci.nexos.arcolive.command.ArCoLIVEClientCommand;
import br.ufal.tci.nexos.arcolive.command.ArCoLIVECommandResult;
import br.ufal.tci.nexos.arcolive.descriptors.ServiceInstanceDescriptor;
import br.ufal.tci.nexos.arcolive.service.connection.ConnectionRequest;

public class GetServiceInstancesCommand extends ArCoLIVEClientCommand {
    
	public GetServiceInstancesCommand(ConnectionRequest request) {
		super(request);
	}
	
	public ArCoLIVECommandResult executeImpl() {
		int serviceId = this.request.getIntegerParameter("serviceId").intValue();
		String response = "";
		
		ServiceInstanceDescriptor serviceInstanceDescriptors[] = ArCoLIVEServiceManager.getInstance().getServiceInstancesDescriptors(serviceId);
		
		for (int i = 0; i < serviceInstanceDescriptors.length; i++) {
			response = response + serviceInstanceDescriptors[i].getServiceInstanceId() + "%" + serviceInstanceDescriptors[i].getName() + "%" + serviceInstanceDescriptors[i].getDescription() + ",";
		}

		if (response.length() > 0) {
			response = response.substring(0, response.length()-1);
		}
	
		return new ArCoLIVECommandResult(this, response);
	}

}